package com.mframe.activiti.task;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

import org.activiti.engine.delegate.DelegateExecution;

import com.mframe.activiti.domain.Audit;
import com.mframe.activiti.domain.AuditDetail;
import com.mframe.activiti.mapper.AuditDetailMapper;
import com.mframe.activiti.mapper.AuditMapper;
import com.mframe.common.utils.spring.SpringBootUtils;

public class MulitiInstanceCompleteTask implements Serializable {

	private static final long serialVersionUID = 1L;

	public boolean completeTask(DelegateExecution execution) {
		int pid = Integer.parseInt(execution.getProcessInstanceId());
		boolean auditorOne = false;
		boolean auditorTwo = false;
		AuditDetailMapper auditDetailMapper = SpringBootUtils.getBean("auditDetailMapper", AuditDetailMapper.class);
		AuditMapper auditMapper = SpringBootUtils.getBean("auditMapper", AuditMapper.class);
		List<AuditDetail> list = auditDetailMapper.findDetailByPid(pid);
		for (AuditDetail li : list) {
			if (li.getStatus() == 1) { // 通过
				if (Objects.equals(li.getUserRole(), "1")) {
					auditorOne = true;
				} else if (Objects.equals(li.getUserRole(), "2")) {
					auditorTwo = true;
				}
			} else {
				if (li.getStatus() == 2) { // 驳回
					Audit audit = new Audit();
					audit.setId(list.get(0).getAuditId());
					audit.setStatus("2");
					auditMapper.updateAudit(audit);
				}
			}
		}
		if (auditorOne && auditorTwo) {
			Audit audit = new Audit();
			audit.setId(list.get(0).getAuditId());
			audit.setStatus("1");
			auditMapper.updateAudit(audit);
		}

		return auditorOne && auditorTwo;
	}
}
